package com.ezcoding.controller;

import com.ezcoding.dao.TradeDao;
import com.ezcoding.dao.UserDao;
import com.ezcoding.model.Trade;
import com.ezcoding.model.User;
import com.ezcoding.service.TradeService;
import com.ezcoding.utils.DateUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Before(Tx.class)
public class TradeController extends Controller {

    private static Log log = LogFactory.getLog(TradeController.class);

    /**
     * 获取支付二维码
     */
    public void getQrCode() {
        String username = getPara("username");
        int day = getParaToInt("day");
        String totalAmount = getPara("totalAmount");

        renderJson(TradeService.getInstance().getQrCode(username, day, totalAmount));
    }

    /**
     * 请求用户信息
     */
    public void queryUserInfo() {
        String username = getPara("username");

        renderJson(TradeService.getInstance().queryUserInfo(username));
    }

    /**
     * 支付完成调用函数
     */
    public void payMethod() {

        Enumeration<String> paraNames = getParaNames();
        Map<String, String> map = new HashMap<>();
        while (paraNames.hasMoreElements()) {
            String paraName = paraNames.nextElement();
            map.put(paraName, getPara(paraName));
            //log.info(paraName + ":" + getPara(paraName));
        }

        String tradeStatus = map.get("trade_status");

        if (Trade.TRADE_STATUS_TRADE_SUCCESS.equals(tradeStatus)) {
            String tradeNo = map.get("trade_no");
            Trade trade = TradeDao.dao.getTrade(tradeNo);

            if (trade != null) {
                return;
            }

            String body = map.get("body");
            String[] bodys = body.split("#");
            String username = bodys[0];
            double totalAmount = Double.parseDouble(map.get("total_amount"));

            trade = new Trade();

            int tradeDay = Integer.parseInt(bodys[1]);

            trade.setTradeNo(tradeNo);
            trade.setSubject(map.get("subject"));
            trade.setUsername(bodys[0]);
            trade.setTradeDay(tradeDay);
            trade.setTotalAmount(totalAmount);

            User user = UserDao.dao.getUserByUsername(username);

            if (user == null) {
                trade.setTradeStatus(Trade.TRADE_STATUS_TRADE_FAILED);
                trade.setTradeMessage(Trade.TRADE_MESSAGE_USER_UNEXIST);
                trade.save();
                return;
            }

            if (!user.updateUpUserCredit(tradeDay)) {
                trade.setTradeStatus(Trade.TRADE_STATUS_TRADE_FAILED);
                trade.setTradeMessage(Trade.TRADE_MESSAGE_UPDATE_UPUSERS_FAILED);
                trade.save();
                return;
            }

            String dateRes = DateUtil.dateAdd(user.getEndDate(), trade.getTradeDay());
            user.setEndDate(dateRes);
            user.update();
            trade.setTradeStatus(Trade.TRADE_STATUS_TRADE_SUCCESS);
            trade.setTradeMessage(Trade.TRADE_MESSAGE_TRADE_SUCCESS);
            trade.save();
        }
    }
}
